

Ext.onReady(function(){
 	initPromptPanel();	
	closeAlarmPrompt(0);
    //showAlarmPrompt();
});


var isAlarmShown = false;
var userClosedAction = false;

function showHideAlarm(){
	if(isAlarmShown){
		closeAlarmPrompt();
		userClosedAction = true;
	}else{
		showAlarmPrompt();
	}
	
}

function initPromptPanel(){
	Ext.get('prompt').setStyle('display','');
	promtPanel = new Ext.Panel({
        title: promtTitle,
        renderTo:Ext.getBody(),
        //html:'test',
        contentEl: 'prompt',
        animCollapse:false,
        cls:'empty',
        height:250,
        width:240,
        tools: [{id:'minimize',handler:function(){closeAlarmPrompt();userClosedAction = true;}},
               {id:'restore',handler:function(){ showAlarmPrompt();}}],
        style: {display:'',position:'absolute',zIndex:1000}
        
    });
	
	promtPanel.on("expand",function(panel){
		isAlarmShown = true;
    });
	
	promtPanel.on("collapse",function(panel){
		isAlarmShown = false;
    });
	//promtPanel.doLayout();
	
	setAlarmOriginalPosition();
	//Ext.get('prompt').setStyle('diplay','');
}

var promtPanel = null;
function showAlarmPrompt(duration){

	if(typeof duration == 'undefined'){
		duration = 2;
    }
	promtPanel.setTitle(promtTitle);
	promtPanel.setWidth(210);
	promtPanel.expand(); 
	
	promtPanel.getTool('restore').setVisible(false);
	promtPanel.getTool('minimize').setVisible(true);
	setAlarmOriginalPosition();
	
	/*
	promt.setStyle('height','250px');
	promt.slideIn('b', { duration: duration, callback:function(){
		isAlarmShown = true;
	} });*/
	
}

function setAlarmOriginalPosition(height){
	
	var bw = document.body.clientWidth;//document.documentElement.clientWidth;
	var bh = document.body.clientHeight + document.body.scrollTop;	
	
	var width = promtPanel.getOuterSize().width + 5;//204;
	
	if(typeof height == 'undefined'){
		height = promtPanel.getOuterSize().height + 5;//254;
	}
	
	//promt.setLeft((bw - width)+'px');
	//promt.setTop((bh - height)+'px');
	promtPanel.setPosition(bw - width, bh - height);

	window.onresize = function(){
		 bw = document.body.clientWidth;
		 bh = document.body.clientHeight + document.body.scrollTop;
		 //promt.setLeft((bw - width)+'px');
		 //promt.setTop((bh - height)+'px');
		 promtPanel.setPosition(bw - width, bh - height);
	};
	
	window.onscroll = function(){
		 bw = document.body.clientWidth;
		 bh = document.body.clientHeight + document.body.scrollTop;
		 //promt.setLeft((bw - width)+'px');
		 //promt.setTop((bh - height)+'px');		
		 promtPanel.setPosition(bw - width, bh - height);
	};
	
}


function clearAlarmprompt(){
	var prompt = Ext.get('prompt');
	prompt.select(".content").update('');
	
}



function insertPromptRow(text){
	var prompt = Ext.get('prompt');

   var div = document.createElement("div");
   div = Ext.get(div);
   div.addClass('divRow');
   div.update(text);
   prompt.select(".content").item(0).appendChild(div);
}

function isAlarmPromt(){
	
	
	
	return false;
}

function closeAlarmPrompt(duration){
	
	promtPanel.collapse(); 
	promtPanel.setTitle('');
	promtPanel.setWidth(30);
	promtPanel.getTool('restore').setVisible(true);
	promtPanel.getTool('minimize').setVisible(false);
	setAlarmOriginalPosition();
	
}

function deleteAlarm(realTimeDataID){
	Ext.Ajax.request({
	    url: './delete_alarming.jsp',
	    params: 'realtimedataid='+realTimeDataID,
	    success:function(res){
			if(res.responseText == 'OK'){
				var prompt = Ext.get('prompt');
				var rows = prompt.select(".divRow");
				for(var i = 0; i < rows.getCount(); i++){
					var el = rows.item(i);
					if(el.contains("table"+realTimeDataID)){
						el.remove(); 
						break;
					}
				}
			}
		}
	});
}

function get_prompt_data(ime, id, date, lat, lng, signal, speed, cmd, realTimeDataID) {
    var j = "";
    if (id > 0) {
        var url = "gps_comment.jsp?id=" + id;
        var l = "javascript:showPopWin('" + url + "', 400, 300)";
    }
    var m = "";
    try{
	    var trackerInfo = "";
	    if (ime != "") {
	        trackerInfo = gpstracker_map[ime].name;
	    }
	    var p = get_gps_cmd_str(cmd);
	    if (cmd > 0) {
	        p = '<span style="color:red;font-weight:bold">' + get_gps_cmd_str(cmd) + '</span>';
	    }
	    //  g_str_lat is defined in realtime.jsp
	    var q = "<table cellpadding=0 cellspacing=0 border=0 id='table"+realTimeDataID+"'><tr><td align='left'><b>" 
	    				+ trackerInfo + '</b><td><div style="cursor:pointer;z-index:50;float:right;width:20px;overflow:hidden;padding-right:20px;height:20px;" onclick="deleteAlarm('+realTimeDataID+')"><img src="images/delete.png" absmiddle="top"/></div></td><tr><td colspan="2" align="left">' + date + "<br/>" + "<b>" 
	    				+ g_str_lat + "</b> " + lat +"  <b>" + g_str_lng + "</b> "+ lng 
	    				+ "        <b>" + g_str_speed + "</b> " + speed  + "<br/><b>" + g_str_state 
	    				+ "</b> " + p + "</td></tr></table>";
	    return q;
    }catch(ex){}
    return "";
};